package com.prp.gwt.client.widgets.mail;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.prp.gwt.client.user.mail.MailListener;
import com.prp.gwt.client.vo.NewMail;
import com.prp.gwt.client.widgets.MessageDialogBox;

public class MassMailPanel extends VerticalPanel {
	private static MassMailPanel instance;
	private NewMail newMail = null;
	//private TextBox addressBox;
	private TextBox titleBox;
	private TextArea textBox;
	
	public static MassMailPanel getInstance() {
		if (instance == null)
			instance = new MassMailPanel();
		return instance;
	}
	private MassMailPanel(){
		newMail = new NewMail();
		init();
	}
	private void init() {
		add(getMailPanel());
		add(getControlPanel());
	}
	public void Load(NewMail newMail){
		this.newMail = newMail;
		clear();
		init();
	}

	public VerticalPanel getMailPanel() {
		VerticalPanel mailPanel = new VerticalPanel();
		mailPanel.setSpacing(5);
		mailPanel.setSize("100%", "80%");

//		HorizontalPanel addressPanel = new HorizontalPanel();
//		mailPanel.add(addressPanel);
//
//		Label addressLabel = new Label("收件人：");
//		addressPanel.add(addressLabel);
//		addressLabel.setWidth("70");
//
//		addressBox = new TextBox();
//		addressPanel.add(addressBox);
//		addressBox.setName("收件");
//		addressBox.setSize("152", "21");
//		addressBox.setText(newMail.getAddress());

		HorizontalPanel titlePanel = new HorizontalPanel();
		mailPanel.add(titlePanel);

		Label titleLabel = new Label("主   题：");
		titlePanel.add(titleLabel);
		titleLabel.setWidth("70");

		titleBox = new TextBox();
		titleBox.setText(newMail.getTitle());
		titlePanel.add(titleBox);

		textBox = new TextArea();
		textBox.setText(newMail.getContent());
		mailPanel.add(textBox);
		textBox.setSize("100%", "179px");

		return mailPanel;
	}

	public HorizontalPanel getControlPanel() {
		HorizontalPanel controlPanel = new HorizontalPanel();
		controlPanel
				.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		controlPanel.setSpacing(5);
		controlPanel.setWidth("100%");

		Button sendButton = new Button("Send");
		sendButton.setHeight("25");
		sendButton.setText("群发");
		sendButton.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				if(titleBox.getText() == null || titleBox.getText().trim().equals("")){
					MessageDialogBox.show("信息错误", "请填写标题！");
					return;
				}
				if(textBox.getText() ==  null || textBox.getText().trim().equals("")){
					MessageDialogBox.show("信息错误", "请填写邮件内容！");
					return;
				}
					
				NewMail newMail = new NewMail(-1, "", 
						titleBox.getText(), textBox.getText());
				MailListener.getInstance().sendMassMail(newMail);
				
			}

		});
		controlPanel.add(sendButton);

		Button cancelButton = new Button("Cancel");
		cancelButton.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				MailListener.getInstance().cancelMail();
				MailView.getInstance().setBarVisible(true);
				MailView.getInstance().showWidget(MailView.INMAILBOX);
			}

		});
		cancelButton.setHeight("25");
		cancelButton.setText("取消");
		controlPanel.add(cancelButton);

		return controlPanel;

	}

}
